<?php
class UsersController extends AppController {

	var $name = 'Users';
	var $helpers = array (
		'Html',
		'Form'
	);
	var $components = array (
		'Auth',
		'SwiftMailer'
	);
	var $uses = array (
		'User',
		'Licitation',
		'Sale'
	);

	function beforeFilter() {
		parent :: beforeFilter();

		if ($this->Admin != 1) {
			$this->Auth->deny('*');
			$this->Auth->allow('login', 'add', 'confirmation_email');
			if ($this->Auth) {
				$this->Auth->allow('profile_infos', 'profile_auctions', 'logout', 'buy_licitations');
			}
		} else {
			$this->Auth->allow('*');
		}
	}

	function index() {
		$this->User->recursive = 1;
		$this->set('users', $this->paginate());
	}

	function login() {
	}
	function logout() {
		$this->Session->del('Permissions');
		$this->redirect($this->Auth->logout());
	}

	function buy_licitations($num) {
		if (!$this->Auth->user('id')) {
			$this->Session->setFlash(__('Please login', true));
			$this->redirect(array (
				'controller' => 'users',
				'action' => 'login'
			));
		}

		App :: import('Model', 'User');
		$user = new User();
		$user->id = $this->Auth->user('id');

		if ($user->saveField('nrLicitations', $this->Auth->user('nrLicitations') - 1)) {
			$this->Session->write('Auth.User.nrLicitations', $this->Auth->user('nrLicitations') + $num);
			//$this->Session->setFlash(__('Pack bought', true));

		}

	}

	function confirmation_email($user_id) {
		$conditions = array (
			'User.id' => $user_id
		);
		$user = $this->User->find('first', array (
			'conditions' => $conditions
		));
		$this->User->id = $user['User']['id'];
		if ($user['User']['active'] == '0') {
			if ($this->User->saveField('active', '1'))
				$this->Session->setFlash(__('User activated', true));
		} else
			$this->Session->setFlash(__('User already activated', true));

		$this->redirect(array (
			'controller' => 'sales',
			'action' => 'index'
		));
	}

	function profile_auctions() {
		$conditions = array (
			'Sale.user_id' => $this->Auth->user('id'),
			"Sale.timeEnd <" => date('Y-m-d  H:i:s')
		);
		$this->set('sales', $this->Sale->find('all', array (
			'conditions' => $conditions
		)));
	}

	function licitationsUsed($sale_id = null) {

		$conditions = array (
			'Licitation.user_id' => $this->Auth->user('id'),
			'Licitation.sale_id' => $sale_id
		);
		echo $this->Licitation->find('count', array (
			'conditions' => $conditions
		));
	}

	function profile_infos($id = null) {

		$id = $this->Auth->user('id');
		echo $id;
		if (!$id && empty ($this->data)) {
			$this->Session->setFlash(__('Invalid User', true));
			$this->redirect(array (
				'action' => 'index'
			));
		}
		if (!empty ($this->data)) {
			//print_r($this->data);

			$this->User->set($this->data);
			if ($this->User->save()) {
				$this->Session->setFlash(__('The User has been saved', true));
				$this->redirect(array (
					'action' => 'index'
				));
			} else {
				$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
			}
		}
		if (empty ($this->data)) {
			$this->data = $this->User->read(null, $id);
			$countries = $this->User->Country->find('list');
			$this->set(compact('countries'));
		}
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid User.', true));
			$this->redirect(array (
				'action' => 'index'
			));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function _sendSimpleMail($email = null, $user_id = null) {
		$this->SwiftMailer->smtpType = 'tls';
		$this->SwiftMailer->smtpHost = 'smtp.gmail.com';
		$this->SwiftMailer->smtpPort = 465;
		$this->SwiftMailer->smtpUsername = 'ricardocanastro@gmail.com';
		$this->SwiftMailer->smtpPassword = '227114353';

		$this->SwiftMailer->sendAs = 'html';
		$this->SwiftMailer->from = 'ricardocanastro@gmail.com';
		$this->SwiftMailer->fromName = 'Bidaki';
		$this->SwiftMailer->to = $email;
		//set variables to template as usual
		$this->set('user_id', $user_id);

		try {
			if (!$this->SwiftMailer->send('confirmation', 'Confirmation Email')) {
				$this->log("Error sending email");
			}
		} catch (Exception $e) {
			$this->log("Failed to send email: " . $e->getMessage());
		}
		$this->redirect($this->referer(), null, true);

	}

	function add() {
		if (!empty ($this->data)) {

			$condition1 = array (
				'login' => $this->data['User']['login']
			);
			$condition2 = array (
				'email_address' => $this->data['User']['email_address']
			);

			if ($this->User->find('count', array (
					'conditions' => $condition1
				)) > 0) {
				$this->Session->setFlash(__('The User could not be saved. Login already in use. Please, try again.', true));
				$this->redirect(array (
					'action' => 'add'
				));
			}

			if ($this->User->find('count', array (
					'conditions' => $condition2
				)) > 0) {
				$this->Session->setFlash(__('The User could not be saved. Email already in use. Please, try again.', true));
				$this->redirect(array (
					'action' => 'add'
				));
			}

			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The User has been saved', true));
				$this->_sendSimpleMail($this->data['User']['email_address'], $this->User->id);
				$this->redirect(array (
					'action' => 'login'
				));
			} else {
				$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
			}
		}
		$countries = $this->User->Country->find('list');
		$this->set(compact('countries'));
	}

	function edit($id = null) {
		if (!$id && empty ($this->data)) {
			$this->Session->setFlash(__('Invalid User', true));
			$this->redirect(array (
				'action' => 'index'
			));
		}
		if (!empty ($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The User has been saved', true));
				$this->redirect(array (
					'action' => 'index'
				));
			} else {
				$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
			}
		}
		if (empty ($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
		$groups = $this->User->Group->find('list');
		$this->set(compact('groups'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for User', true));
			$this->redirect(array (
				'action' => 'index'
			));
		}
		if ($this->User->del($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array (
				'action' => 'index'
			));
		}
	}

}
?>